<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>
                
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="店铺" prop="shop_name" min-width="140"></el-table-column>
                <el-table-column label="执行时间区间" prop="date_zone" min-width="240"></el-table-column>
                <el-table-column label="消息内容" prop="message" min-width="240"></el-table-column>
                <!-- <el-table-column label="操作数量" prop="nums"></el-table-column> -->
                
                <el-table-column label="操作" min-width="160" fixed="right">
                    <template slot-scope="scope">
                        <!-- <el-button el-button type="text" icon="el-icon-search" @click="showDetailDialog(scope.row, 'view')">查看日志</el-button> -->
                        <el-button el-button type="text" icon="el-icon-search" @click="executeRow(scope.row)">执行</el-button>
                        <el-button el-button type="text" icon="el-icon-edit" @click="showEditDialog(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="日志记录" min-width="160" fixed="right">
                    <template slot-scope="scope">
                        <el-button el-button type="text" icon="el-icon-search" @click="showDetailDialog(scope.row, 'view')">查看日志</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
        <edit-dialog v-model="editDialog.visible" v-if="editDialog.visible" :params="editDialog.data" @success="handleQuery"></edit-dialog>
    </div>
</template>

<script>
import { getList, messageSync, getShopList } from '@/api/customer'
import { STATUS_LIST } from './constant'
import { exportFile } from '@/utils/index'
import DetailDialog from './components/DetailDialog.vue'
import EditDialog from './components/EditDialog.vue'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'CustomerMessageList',
    components: { DetailDialog, EditDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                create_time: undefined,
                shop_id: undefined,
            },
            detailDialog: {
                visible: false,
                data: {},
                type: ''
            },
            editDialog: {
                visible: false,
                data: {},
            },
            shopList: [],
        }
    },
    async created() {
        this.loading = true
        try {
            const result = await getShopList()
            this.shopList = result?.data ?? []
        } catch (error) {
            console.log(error)
            this.loading = false
        }
        this.getList()
    },
    methods: {
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
          (qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },

        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        showEditDialog(row) {
            this.editDialog.visible = true
            this.editDialog.data = row
        },
        executeRow(row) {
            messageSync({id: row.id}).then((res) => {
                this.$message.success(res.msg)
                this.handleQuery()
            });
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date !== undefined) {
                qyparams.date = parseTime(qyparams.date, 'y-m')
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            getList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
